//
//  QuotationViewController.swift
//  YUANSHI
//
//  Created by pro on 2019/12/7.
//  Copyright © 2019 wangbin. All rights reserved.
//

import UIKit
//import MJRefresh
import SVProgressHUD
class QuotationViewController: WBBaseViewController {

    private let wbQuotaCellID = "QuotaCollectionViewCell"
    private let webXibQuotaCellID = "QuotaXibCollectionViewCell"
    
    private let wbQuaHeaderId = "QuotaHeaderCollectionReusableView"
    private let wbQuaFooderId = "QuotaFooterCollectionReusableView"
    private let wbQuaXibHeaderID = "QuotaXibHeaderCollectionReusableView"
    lazy var collectionView : UICollectionView = {

        let layout = UICollectionViewFlowLayout.init();
        layout.itemSize = CGSize(width: (WBScreenWidth-60)/2, height: 100)
        layout.minimumLineSpacing = 20 //垂直每个item的间隔距离
        layout.minimumInteritemSpacing = 20 //水平每个item的间隔距离
        layout.headerReferenceSize = CGSize(width: 375, height: 160)//设置组头部
        layout.footerReferenceSize = CGSize(width: WBScreenWidth, height: 160)//设置足部
        layout.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        let collection = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collection.backgroundColor = UIColor.yellow
        collection.showsHorizontalScrollIndicator = true;
        collection.delegate = self
        collection.dataSource = self
        collection.register(QuotaCollectionViewCell.self, forCellWithReuseIdentifier: wbQuotaCellID)
//        collection.mj_header = MJRefreshHeader.init()
        collection.register(UINib.init(nibName: "QuotaXibCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: webXibQuotaCellID)
        collection.register(QuotaHeaderCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: wbQuaHeaderId)
        collection.register(QuotaFooterCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: wbQuaFooderId)
        collection.register(UINib.init(nibName: "QuotaXibHeaderCollectionReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: wbQuaXibHeaderID)
//        collectionView.register(UINib(nibName:headerlName, bundle:nil), forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier:headerlName)


//        返回的是 UICollectionReusableView
//        collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "kHeaderViewID")
//
//        返回的是xib
//        collectionView.register(UINib(nibName: "CollectionHeaderView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "kHeaderViewID")

        
        return collection
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.yellow
        self.setUI()
        
    }

    
    func setUI() {
        self.view.addSubview(self.collectionView)
        
        self.collectionView.snp.makeConstraints { (make) in
            
            make.left.equalToSuperview()
            make.right.top.bottom.equalToSuperview()
        }
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5) {
//            SVProgressHUD.show()
        }
      
        
    }
}

extension QuotationViewController : UICollectionViewDelegate, UICollectionViewDataSource {

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 8
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell : QuotaCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: wbQuotaCellID, for: indexPath) as! QuotaCollectionViewCell
        cell.conStr = String(format: "自定义cell%d",indexPath.row)
        
        let xibCell : QuotaXibCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: webXibQuotaCellID, for: indexPath) as! QuotaXibCollectionViewCell
        xibCell.contitLabel.text = String(format: "xib自定义cell%d",indexPath.row)
        return indexPath.row%2 == 0 ? cell : xibCell
        
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
//
        if (kind as String) == UICollectionView.elementKindSectionHeader  {
            //返回头部
            print(kind)
//            if indexPath.section%2 == 0 {
                let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: wbQuaHeaderId, for: indexPath) as! QuotaHeaderCollectionReusableView

                headerView.conStr = String(format: "代码自定义头部%d",indexPath.section)
                return headerView
//            }else {
//                 let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "kHeaderViewID", for: indexPath)
//            let xibHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: wbQuaXibHeaderID, for: indexPath)
//                xibHeaderView.conStr = String(format: "代码自定义头部%d",indexPath.section)
                
//                return  xibHeaderView
//            }
            
            
            
        }else
        {
            //返回底部
            let fooderView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: wbQuaFooderId, for: indexPath) as! QuotaFooterCollectionReusableView
            fooderView.conStr = String(format: "代码自定义足部%d",indexPath.section)
            return fooderView
        }
        
    }
    
    
    
    
}
